package com.example.waimaiandroid.OrderDetailActivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.waimai.pojo.Food;
import com.example.waimaiandroid.R;


import java.util.List;

public class OrderDetailAdapter extends BaseAdapter {
    // 数据
    private List<Food> orderFoods;
    private Context mContext;
    private LayoutInflater mInflater;

    public OrderDetailAdapter(List<Food> orderFoods, Context context){
        this.orderFoods = orderFoods;
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    // 创建视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(null == convertView){
            // 创建
            convertView = mInflater.inflate(R.layout.order_food_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder  = (ViewHolder) convertView.getTag();
        }
        // 填充数据
        // 利用 Glide 填充图片
        Glide.with(convertView).load(orderFoods.get(position).getFoodImage()).into(holder.mOrderFoodImage);
        holder.mOrderFoodName.setText(orderFoods.get(position).getFoodName());
        holder.mOrderFoodNum.setText(String.valueOf(orderFoods.get(position).getAddNums()));
        holder.mOrderFoodPrice.setText(String.valueOf(orderFoods.get(position).getAddNums()* orderFoods.get(position).getFoodPrice()));

        return convertView;
    }

    @Override
    public int getCount() {
        return orderFoods.size();
    }

    @Override
    public Object getItem(int position) {
        return orderFoods.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // 创建ListView 每一项对象
    class ViewHolder{
        ImageView mOrderFoodImage;
        TextView mOrderFoodName;
        TextView mOrderFoodNum;
        TextView mOrderFoodPrice;

        public ViewHolder(View view){
            mOrderFoodImage = view.findViewById(R.id.orderFoodImage);
            mOrderFoodName = view.findViewById(R.id.orderFoodName);
            mOrderFoodNum = view.findViewById(R.id.orderFoodNum);
            mOrderFoodPrice = view.findViewById(R.id.orderFoodPrice);
        }

    }

}
